package org.example.struct.list.basicMethod;

import com.alibaba.fastjson2.JSON;
import org.example.struct.list.Node;

/**
 * @author liyishan
 * @date 2023/7/13 14:11
 * @apiNote
 */

public class reverser {
    public static void main(String[] args) {
        Node<Object> head = new Node<>(1);
        head.next = new Node<>(2);
        head.next.next = new Node<>(3);
        head.next.next.next = new Node<>(4);
        head.next.next.next.next = new Node<>(5);
        head.next.next.next.next.next = new Node<>(6);
        head.next.next.next.next.next.next = new Node<>(7);
        Node<Object> result= reverse(head);
        System.out.println(JSON.toJSON(result));

    }

    //反转链表
    public static Node<Object> reverse(Node<Object> head) {
        if (head == null || head.next == null) {
            return head;
        }
        Node<Object> next = head.next;
        Node<Object> reverse = reverse(next);
        next.next = head;
        head.next = null;
        return reverse;
    }

}
